package com.liuyk.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.liuyk.controller.CompressController;
import com.liuyk.main.activity.R;
import com.liuyk.util.AnimationUtil;
import com.liuyk.util.DeviceUtil;

import java.io.File;

public class MainActivity extends BaseActivity implements View.OnClickListener {
    private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
    // We want at least 2 threads and at most 4 threads in the core pool,
    // preferring to have 1 less than the CPU count to avoid saturating
    // the CPU with background work
    private static final int CORE_POOL_SIZE = Math.max(2, Math.min(CPU_COUNT - 1, 4));
    private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;

    private float mRotateBtnAngle_01 = 0f;
    private float mRotateBtnAngle_02 = 0f;
    private Button mRotateBtn01;
    private Button mRotateBtn02;
    private RelativeLayout mRotateParent;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i(TAG, "onCreate");
        init();
        checkPermissions();

        ffmpeg();

        showToast(getString(R.string.res_str));
    }

    private void ffmpeg() {
        File file = new File(Environment.getExternalStorageDirectory(), "vi.mp4");
        CompressController.getInstance(this).compress(file.getAbsolutePath(), new CompressController.CompressCallBack() {
            @Override
            public void onCompressSuccess(String message) {

            }

            @Override
            public void onCompressFail(String message) {

            }

            @Override
            public void onCompressProgress(String message) {

            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i(TAG, "onStart");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.i(TAG, "onRestart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "onResume");
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        Log.i(TAG, "onAttachedToWindow");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i(TAG, "onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.i(TAG, "onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy");
    }

    public void init() {
        //进程
        findViewById(R.id.messenger).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, MessengerActivity.class));
            }
        });
        //压缩
        findViewById(R.id.compress).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, CompressActivity.class));
            }
        });

        findViewById(R.id.anmi).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, AnmiActivity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(i);
            }
        });

        findViewById(R.id.handler).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, HandlerActivity.class));
            }
        });

        TextView info = findViewById(R.id.info);
        info.append("CPU_COUNT: " + CPU_COUNT + " CORE_POOL_SIZE: " + CORE_POOL_SIZE + " MAXIMUM_POOL_SIZE: " + MAXIMUM_POOL_SIZE);
        info.append("AndroidId: " + DeviceUtil.getAndroidId(this));

        mRotateBtn01 = findViewById(R.id.rotate_view01);
        mRotateBtn02 = findViewById(R.id.rotate_view02);
        AnimationUtil.rotateX(mRotateBtn02, mRotateBtnAngle_02, mRotateBtnAngle_02 + 180f);
        AnimationUtil.alpha(mRotateBtn02, 1f, 0f);
        mRotateParent = findViewById(R.id.rotate_parent);

        //https://rbs.9f.cn/file/html/20200222/16691506.html?pageId=1669&channel=QB_ACTIVITY_CHANNEL&productCode=QB&utm_medium=&utm_campaign=&utm_source=QB_ACTIVITY_CHANNEL

        findViewById(R.id.skin).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, SkinActivity.class));
            }
        });
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rotate_parent:
                mRotateParent.setOnClickListener(null);

                mRotateBtnAngle_02 = mRotateBtnAngle_02 + 180f;

                AnimationUtil.rotateX(mRotateBtn01, mRotateBtnAngle_01, mRotateBtnAngle_01 + 180f);
                AnimationUtil.rotateX(mRotateBtn02, mRotateBtnAngle_02, mRotateBtnAngle_02 + 180f);

                float mRotateBtnAlpha_01 = mRotateBtn01.getAlpha() > 0 ? 1f : 0f;
                AnimationUtil.alpha(mRotateBtn01, mRotateBtnAlpha_01, Math.abs(mRotateBtnAlpha_01 - 1f));

                float mRotateBtnAlpha_02 = mRotateBtn02.getAlpha() > 0 ? 1f : 0f;
                AnimationUtil.alpha(mRotateBtn02, mRotateBtnAlpha_02, Math.abs(mRotateBtnAlpha_02 - 1f));

                mRotateBtnAngle_01 = mRotateBtnAngle_01 + 180f;
                break;

            default:
                break;
        }
    }

    private void stereoscopicAnim(View view) {
//        AnimationUtil.rotate(view);
        AnimationUtil.rotateXY(view);
//        AnimationUtil.y(view);
//        AnimationUtil.translationY(view);
//        AnimationUtil.g(view);
//        AnimationUtil.setTargetProperty(view);
//        AnimationUtil.scale(view);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }
}
